package com.dz.risk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dz.risk.common.SysConstant;
import com.dz.risk.common.exception.RRException;
import com.dz.risk.dao.DebtBasicInformationCalculationDao;
import com.dz.risk.pojo.QueryParams;
import com.dz.risk.pojo.domain.*;
import com.dz.risk.pojo.model.PageModel;
import com.dz.risk.service.BasicBondInformationService;
import com.dz.risk.service.DebtBasicInformationCalculationService;
import com.dz.risk.service.ProprietaryHoldingBondInformationService;
import com.dz.risk.service.ProprietaryValuationOfBondHoldingsInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Service("debtBasicInformationCalculationService")
public class DebtBasicInformationCalculationServiceImpl extends ServiceImpl<DebtBasicInformationCalculationDao, DebtBasicInformationCalculationEntity> implements DebtBasicInformationCalculationService {
    @Autowired
    private DebtBasicInformationCalculationDao debtBasicInformationCalculationDao;
    @Autowired
    private ProprietaryHoldingBondInformationService proprietaryHoldingBondInformationService;
    @Autowired
    private ProprietaryValuationOfBondHoldingsInformationService proprietaryValuationOfBondHoldingsInformationService;
    @Autowired
    private BasicBondInformationService basicBondInformationService;
    @Autowired
    private DebtBasicInformationCalculationService debtBasicInformationCalculationService;

    @Override
    public IPage<DebtBasicInformationCalculationEntity> query( QueryParams queryParams) {
        IPage<DebtBasicInformationCalculationEntity> page = new Page<>();
        page.setCurrent(queryParams.getPageNum());
        page.setSize(queryParams.getPageSize());
        QueryWrapper<DebtBasicInformationCalculationEntity> wrapper = new QueryWrapper<DebtBasicInformationCalculationEntity>();
        wrapper.lambda()
                .eq(!StringUtils.isEmpty(queryParams.getBondCode()),DebtBasicInformationCalculationEntity::getBondCode, queryParams.getBondCode())
                .eq(!StringUtils.isEmpty(queryParams.getBondType()),DebtBasicInformationCalculationEntity::getBondType, queryParams.getBondType())
                .eq(!StringUtils.isEmpty(queryParams.getVersion()),DebtBasicInformationCalculationEntity::getVersion, queryParams.getVersion())
                .like(!StringUtils.isEmpty(queryParams.getBondName()),DebtBasicInformationCalculationEntity::getBondName, queryParams.getBondName())
                .eq(!StringUtils.isEmpty(queryParams.getBeginDate()),DebtBasicInformationCalculationEntity::getBeginDate, queryParams.getBeginDate())
                .eq(!StringUtils.isEmpty(queryParams.getEndDate()),DebtBasicInformationCalculationEntity::getEndDate, queryParams.getEndDate())
                .orderByDesc(DebtBasicInformationCalculationEntity::getVersion);

        IPage<DebtBasicInformationCalculationEntity> debtBasicInformationCalculationEntityIPage = debtBasicInformationCalculationDao.selectPage(page, wrapper);
        return debtBasicInformationCalculationEntityIPage;
    }

    @Override
    public void roll(Long version,Integer bondType) throws RRException {
        List<BasicBondInformationEntity> basicBondInformationEntityList= basicBondInformationService.selectByVersion(version,bondType);
        //自营持有债券信息
        List<ProprietaryHoldingBondInformationEntity> proprietaryHoldingBondInformationEntities = proprietaryHoldingBondInformationService.queryByVersion(version);
        //自营估值债券持仓信息
        List<ProprietaryValuationOfBondHoldingsInformationEntity> proprietaryValuationOfBondHoldingsInformationEntities = proprietaryValuationOfBondHoldingsInformationService.queryByVersion(version);
        //集合
        List<DebtBasicInformationCalculationEntity> debtBasicInformationCalculationEntities = new ArrayList<>();
        for (BasicBondInformationEntity basicBondInformationEntity : basicBondInformationEntityList) {
            DebtBasicInformationCalculationEntity debtBasicInformationCalculationEntity = new DebtBasicInformationCalculationEntity();
            debtBasicInformationCalculationEntity.setId(null);
            //债券名称
            debtBasicInformationCalculationEntity.setBondName(basicBondInformationEntity.getBondName());
            //债券代码
            debtBasicInformationCalculationEntity.setBondCode(basicBondInformationEntity.getBondCode());
            //开始时间
            debtBasicInformationCalculationEntity.setBeginDate(basicBondInformationEntity.getBeginDate());
            //结束时间
            debtBasicInformationCalculationEntity.setEndDate(basicBondInformationEntity.getEndDate());
            //版本号
            debtBasicInformationCalculationEntity.setVersion(version);
            //类型
            debtBasicInformationCalculationEntity.setBondType(bondType);

            //包销金额
            if (basicBondInformationEntity.getExclusiveSalesAmount() != null) {
                debtBasicInformationCalculationEntity.setExclusiveSalesAmount(basicBondInformationEntity.getExclusiveSalesAmount());
            }

            //成本
            if (basicBondInformationEntity.getCost() != null) {
                debtBasicInformationCalculationEntity.setCost(basicBondInformationEntity.getCost());
            }

            //通过流的方式获取对象
            List<ProprietaryHoldingBondInformationEntity> proprietaryHoldingBondInformationList = proprietaryHoldingBondInformationEntities.stream().filter(w -> String.valueOf(w.getStockCode()).equals(basicBondInformationEntity.getBondCode())).collect(Collectors.toList());
            List<ProprietaryValuationOfBondHoldingsInformationEntity> proprietaryValuationOfBondHoldingsInformationEntityList = proprietaryValuationOfBondHoldingsInformationEntities.stream().filter(w -> String.valueOf(w.getStockCode()).equals(basicBondInformationEntity.getBondCode())).collect(Collectors.toList());

            //市值
            if (proprietaryValuationOfBondHoldingsInformationEntityList.size() > 0) {
                //自营估值债券持仓信息
                ProprietaryValuationOfBondHoldingsInformationEntity proprietaryValuationOfBondHoldingsInformationEntity = proprietaryValuationOfBondHoldingsInformationEntityList.get(0);
                if (proprietaryValuationOfBondHoldingsInformationEntity.getFullValue() != null) {
                    debtBasicInformationCalculationEntity.setFullValue(proprietaryValuationOfBondHoldingsInformationEntity.getFullValue());
                }
            }
            if (proprietaryHoldingBondInformationList.size() > 0) {
                //自营持有债券信息
                ProprietaryHoldingBondInformationEntity proprietaryHoldingBondInformationEntity = proprietaryHoldingBondInformationList.get(0);
                //数量
                if (proprietaryHoldingBondInformationEntity.getNumberOfBonds() != null) {
                    debtBasicInformationCalculationEntity.setNumberOfBonds(proprietaryHoldingBondInformationEntity.getNumberOfBonds());
                }
                //信用评级
                if (proprietaryHoldingBondInformationEntity.getRate() != null) {
                    debtBasicInformationCalculationEntity.setRate(proprietaryHoldingBondInformationEntity.getRate());
                }
            }
            debtBasicInformationCalculationEntities.add(debtBasicInformationCalculationEntity);
        }
        //删除同一天拥有的数据
        deleteByversion(version,bondType);
        //新增
        debtBasicInformationCalculationService.saveBatch(debtBasicInformationCalculationEntities);
    }

    /**
     * 删除同一天拥有的数据
     * @param version
     * @param bondType
     */
    private void deleteByversion(Long version, Integer bondType){

        debtBasicInformationCalculationDao.deleteByversion(version,bondType);
    }
}